package com.fr.report.script.function;

import com.fr.base.FRContext;
import com.fr.base.core.antlr.ANTLRException;
import com.fr.report.script.Calculator;
import com.fr.report.script.DelayedFunction;
import com.fr.report.script.Function;
import com.fr.report.script.Function.Type;
import com.fr.report.script.ParameterMapNameSpace;
import com.fr.report.script.Primitive;
import com.fr.report.script.core.FArray;
import com.fr.report.script.core.parser.Expression;
import com.fr.report.script.core.parser.UtilEvalError;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class GREPARRAY extends DelayedFunction
{
  public Object run(Object[] paramArrayOfObject)
  {
    Expression localExpression;
    if (paramArrayOfObject.length != 2)
      return Primitive.ERROR_VALUE;
    Object localObject1 = paramArrayOfObject[0];
    if (!(localObject1 instanceof FArray))
      return Primitive.ERROR_VALUE;
    Calculator localCalculator = getCalculator();
    FArray localFArray1 = (FArray)localObject1;
    try
    {
      localExpression = localCalculator.parse(paramArrayOfObject[1]);
    }
    catch (ANTLRException localANTLRException)
    {
      FRContext.getLogger().log(Level.WARNING, localANTLRException.getMessage(), localANTLRException);
      return localFArray1;
    }
    FArray localFArray2 = new FArray();
    for (int i = 0; i < localFArray1.length(); ++i)
    {
      HashMap localHashMap = new HashMap();
      localHashMap.put("item", localFArray1.elementAt(i));
      localHashMap.put("index", new Integer(i + 1));
      ParameterMapNameSpace localParameterMapNameSpace = ParameterMapNameSpace.create(localHashMap);
      localCalculator.pushNameSpace(localParameterMapNameSpace);
      try
      {
        Object localObject2 = localExpression.eval(localCalculator);
        if ((localObject2 instanceof Boolean) && ((Boolean)localObject2 == Boolean.TRUE))
          localFArray2.add(localFArray1.elementAt(i));
      }
      catch (UtilEvalError localUtilEvalError)
      {
        FRContext.getLogger().log(Level.INFO, localUtilEvalError.getMessage(), localUtilEvalError);
      }
      localCalculator.removeNameSpace(localParameterMapNameSpace);
    }
    return localFArray2;
  }

  public Function.Type getType()
  {
    return Function.ARRAY;
  }

  public String getCN()
  {
    return "GREPARRAY(array，fn):函数(返回true或者false)是条件，过滤此数组，最后形成一个新数组。\n示例：\nGREPARRAY([3,4,2,3,6,8,7], item != 3)等于[4,2,6,8,7].\n";
  }

  public String getEN()
  {
    return "";
  }
}